﻿package kit4j.tests.jdk.lang.string;

import java.nio.charset.Charset;
import java.util.Locale;
import java.util.ResourceBundle;

import kit4j.util.io.ResourceBundleUtil;

public class TestFormatter {

	/**
	 * 测试结果：按字符数对齐不能保证所有数据一定会对齐，使用不同字体或者字符显示方式，不同字符(如中文与英文)的显示宽度可能不统一，会造成宽度误差。
	 * 结论：如果要使字符绝对对齐，应尽可能保证字符按指定像素(或其他类似方式)对象，而不是按字符数量对齐。
	 * @param args
	 */
	public static void main(String[] args) {
		ResourceBundle resource = ResourceBundle.getBundle("receipt",
				Locale.CHINA);
		Receipt re = new Receipt();
		initReceipt(re, resource);

		testPrint(re);
	}

	private static void testPrint(Receipt re) {
		re.printTitle();
		re.print("hongsedel1.1.1098GGGGG", 3, 10.12);
		re.print("Design Pattern", 1, 36.21);
		re.print("Java", 1, 88.12);
		re.printTotal();
	}

	private static void initReceipt(Receipt re, ResourceBundle resource) {
		Charset cs = Charset.forName("UTF-8");
		re.setItemTitle(ResourceBundleUtil.getString(resource, "itemTitle", cs));
		re.setQuantityTitle(ResourceBundleUtil.getString(resource,
				"quantityTitle", cs));
		re.setPriceTitle(ResourceBundleUtil.getString(resource, "priceTitle",
				cs));
		re.setTotalTitle(ResourceBundleUtil.getString(resource, "totalTitle",
				cs));
		re.setItemWidth(Integer.valueOf(ResourceBundleUtil.getString(resource,
				"itemWidth", cs)));
		re.setItemMaxSize(Integer.valueOf(ResourceBundleUtil.getString(
				resource, "itemMaxSize", cs)));
		re.setQuantityWidth(Integer.valueOf(ResourceBundleUtil.getString(
				resource, "quantityWidth", cs)));
		re.setPriceWidth(Integer.valueOf(ResourceBundleUtil.getString(resource,
				"priceWidth", cs)));
		re.setPricePrecision(Integer.valueOf(ResourceBundleUtil.getString(
				resource, "pricePrecision", cs)));
	}

}
